//
//  NSString+Extension.m
//  VirtualCurrency
//
//  Created by apple on 2021/7/5.
//

#import "NSString+Extension.h"

@implementation NSString (Extension)
/**
 根据self字符串和文字大小返回宽高
 
 @param font 字体大小
 
 @return 字体宽高
 */
- (CGSize)getCharactersWithFont:(UIFont *)font
{
    return [self boundingRectWithSize:CGSizeMake(IDWidth, MAXFLOAT) options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:font} context:NULL].size;
}

//根据字符串以及字体大小,最大size,返回实际size
- (CGSize)getSizeWithFont:(UIFont *)font maxSize:(CGSize)maxSize
{
    CGSize size = [self boundingRectWithSize:maxSize options:NSStringDrawingUsesFontLeading | NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName:font,} context:NULL].size;
    return size;
}

+ (BOOL)isValidateEmail:(NSString *)email {
    NSString *emailRegex = @"[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{1,4}";
    return [self isMatchingWithRegularExpression:emailRegex text:email];
}

+ (BOOL)isValidateMobile:(NSString *)mobile {
    NSString *mobileRegex = @"^1(3|4|5|7|8)\\d{9}$";
    return [self isMatchingWithRegularExpression:mobileRegex text:mobile];
}

+ (BOOL)isValidateFixedTelephone:(NSString *)fixedTelephone {
    NSString *fixedTelephoneRegex = @"^(0[0-9]{2})\\d{8}$";
    return [self isMatchingWithRegularExpression:fixedTelephoneRegex text:fixedTelephone];
}

+ (BOOL)isMatchingWithRegularExpression:(NSString *)regularExpression text:(NSString *)text {
    NSPredicate *test = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regularExpression];
    return [test evaluateWithObject:text];
}




+ (NSString *)getPhoneVersion {
    NSString* phoneVersion = @"";
    phoneVersion = [[UIDevice currentDevice] systemVersion];
    return phoneVersion;;
}

+(NSAttributedString *)getAttributeWith:(id)sender
string:(NSString *)string
orginFont:(CGFloat)orginFont
orginColor:(UIColor *)orginColor
attributeFont:(CGFloat)attributeFont
attributeColor:(UIColor *)attributeColor
{
    __block  NSMutableAttributedString *totalStr = [[NSMutableAttributedString alloc] initWithString:string];
    [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:orginFont] range:NSMakeRange(0, string.length)];
    [totalStr addAttribute:NSForegroundColorAttributeName value:orginColor range:NSMakeRange(0, string.length)];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc] init];
    [paragraphStyle setLineSpacing:5.0f]; //设置行间距
    [paragraphStyle setLineBreakMode:NSLineBreakByTruncatingTail];
    [paragraphStyle setAlignment:NSTextAlignmentCenter];
    [paragraphStyle setLineBreakMode:NSLineBreakByCharWrapping];
    [totalStr addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0, [totalStr length])];
    
    if ([sender isKindOfClass:[NSArray class]]) {
        
        __block NSString *oringinStr = string;
        __weak typeof(self) weakSelf = self;
        
        [sender enumerateObjectsUsingBlock:^(NSString *  _Nonnull str, NSUInteger idx, BOOL * _Nonnull stop) {
            
            NSRange range = [oringinStr rangeOfString:str];
            [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
            [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
            oringinStr = [oringinStr stringByReplacingCharactersInRange:range withString:[weakSelf getStringWithRange:range]];
        }];
        
    }else if ([sender isKindOfClass:[NSString class]]) {
        
        NSRange range = [string rangeOfString:sender];
        
        [totalStr addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:attributeFont] range:range];
        [totalStr addAttribute:NSForegroundColorAttributeName value:attributeColor range:range];
    }
    return totalStr;
}
+ (NSString *)getStringWithRange:(NSRange)range
{
    NSMutableString *string = [NSMutableString string];
    for (int i = 0; i < range.length ; i++) {
        [string appendString:@" "];
    }
    return string;
}

//获取当前时间
+ (NSString *)currentDateForDateSeconds{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSString *destDateString = [dateFormatter stringFromDate:[NSDate date]];
    return destDateString;
}

/**
 * 金额的格式转化
 * str : 金额的字符串
 * numberStyle : 金额转换的格式
 * return  NSString : 转化后的金额格式字符串
 *
 * 94863
  * NSNumberFormatterNoStyle = kCFNumberFormatterNoStyle,
  
  * 94,862.57
  * NSNumberFormatterDecimalStyle = kCFNumberFormatterDecimalStyle,
  
  * ￥94,862.57
  * NSNumberFormatterCurrencyStyle = kCFNumberFormatterCurrencyStyle,
  
  * 9,486,257%
  * NSNumberFormatterPercentStyle = kCFNumberFormatterPercentStyle,
  
  * 9.486257E4
  * NSNumberFormatterScientificStyle = kCFNumberFormatterScientificStyle,
  
  * 九万四千八百六十二点五七
  * NSNumberFormatterSpellOutStyle = kCFNumberFormatterSpellOutStyle
 */
+ (NSString *)stringChangeMoneyWithStr:(NSString *)str{
    
    // 判断是否null 若是赋值为0 防止崩溃
    if (([str isEqual:[NSNull null]] || str == nil)) {
        str = 0;
    }
    
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc]init];
    formatter.numberStyle = kCFNumberFormatterDecimalStyle;
    // 注意传入参数的数据长度，可用double
    NSString *money = [formatter stringFromNumber:[NSNumber numberWithDouble:[str doubleValue]]];
    
    return money;
}
@end
